import csv
import logging
def save_augmented_data_to_csv(data, filename):
    """保存增强后的数据"""
    fieldnames = [
        'id', 'text', 'keywords', 'sentiment', 
        'augmentation_type', 'original_id'
    ]
    
    try:
        with open(filename, 'w', newline='', encoding='utf-8-sig') as csvfile:
            writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
            writer.writeheader()
            
            for post in data:
                row = {
                    'id': post.get('id', ''),
                    'text': post.get('text', ''),
                    'keywords': ';'.join(post.get('keywords', [])),
                    'sentiment': post.get('sentiment', 0.0),
                    'augmentation_type': post.get('augmentation', 'none'),
                    'original_id': post.get('original_id', post.get('id', ''))
                }
                writer.writerow(row)
                
        return True
    except Exception as e:
        logging.error(f"增强数据保存失败: {str(e)}")
        return False